package com.erbao.dp.chainofresponsibility;

import java.util.ArrayList;
import java.util.List;

class Request{
    String str;
}

class Response{
    String str;
}

class FilterChain{
    private int index;
    List<Filter> filterList = new ArrayList();

    public boolean registerFilter (Filter filter){
        return filterList.add(filter);
    }

    public void doFilter (Request request, Response response){
        if (index == filterList.size()) return;

        Filter f = filterList.get(index++);
        f.doFilter(request, response, this);
    }

}

interface Filter{
    void doFilter (Request request, Response response, FilterChain chain);
}

class HtmlFilter implements Filter {

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        request.str = request.str.replaceAll("<", "[").replaceAll(">", "]").concat(" HTML Filter()");
        chain.doFilter(request, response);
        response.str = response.str.concat("--  HTML Filter()");
    }
}

class SensitiveFilter implements Filter{

    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        request.str = request.str.replaceAll("996", "995").concat(" SensitiveFilter()");
        chain.doFilter(request, response);
        response.str = response.str.concat(" --SensitiveFilter() ");
    }
}
/**
 * @author CaoBaoYin
 * @since 21/9/26 17:27
 */
public class Main {

    public static void main(String[] args) {
        Request request = new Request();
        request.str = "大家好:),我是一名程序员，每周都是<996>。";
        Response response = new Response();
        response.str = "response";

        FilterChain chain = new FilterChain();
        chain.registerFilter(new HtmlFilter());
        chain.registerFilter(new SensitiveFilter());
        chain.doFilter(request, response);
        System.out.println(request.str);
        System.out.println(response.str);
    }

}
